package com.zyw.customcalendar;

import com.zyw.customcalendar.event.EventIndex;
import com.zyw.customcalendar.model.RealWeather;
import com.zyw.customcalendar.operation.WeartherManager;
import com.zyw.customcalendar.view.CalendarLayout;
import com.zyw.customcalendar.view.CalendarView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;


import de.greenrobot.event.EventBus;


public class MainActivity extends Activity{

	private ImageView btnXinJian;
	private CalendarLayout mCalendarLayout;
	private TextView mWeatherText;
	private TextView mWeatherCity;
	public static final int REQUEST_WEATHER_CODE = 100;
	public static final int REQUEST_CALENDAR_CODE = 200;

	private TextView mMonthOfYaer;

	private EventManager mEventManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnXinJian = (ImageView) findViewById(R.id.btn_xinjian);
		btnXinJian.setOnClickListener(setOnClickListener());

		mMonthOfYaer = (TextView) findViewById(R.id.year_and_month);
		mCalendarLayout = (CalendarLayout) findViewById(R.id.calendar_layout);
		mCalendarLayout.setMonthOfYear(mMonthOfYaer);

		mWeatherText = (TextView) findViewById(R.id.weather_text);
		mWeatherCity = (TextView) findViewById(R.id.weather_city);
		mWeatherCity.setOnClickListener(setOnClickListener());

		EventBus.getDefault().register(this);

		String cityName = (String)AppApplication.getInstance().getSharePreferencesValue("city_name", "北京");
		updateWeatherInformation(cityName);

		mEventManager = new EventManager(MainActivity.this);
	}

	/*
	 * ��ť����¼�
	 * */
	private OnClickListener setOnClickListener(){
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				if(btnXinJian.equals(view)){
					CalendarView currentView = (CalendarView) mCalendarLayout.getViewPager().getCurrentView();
					String selectDaysString = currentView.getCalendarDraw().
							getCalendarOperation().returnSelectDays();
					String selectTimesString = currentView.getCalendarDraw().
							getCalendarOperation().returnCurrentTimes();
					Intent intent = new Intent(MainActivity.this, EventActivity.class);
					intent.putExtra("selectDaysString", selectDaysString);
					intent.putExtra("selectTimesString", selectTimesString);
					startActivityForResult(intent, REQUEST_CALENDAR_CODE);
				}else if(mWeatherCity.equals(view)){
					Intent intent = new Intent(MainActivity.this, CityMianActivity.class);
					intent.putExtra("city_name", mWeatherCity.getText());
					startActivityForResult(intent, REQUEST_WEATHER_CODE);
				}
			}
		};
		return listener;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
		}
	};

	public void onEventMainThread(Message message){
		if(message.what == EventIndex.REFRASH_TAG_INDEX){
			mCalendarLayout.showCurrentView();
		}else if(message.what == EventIndex.MODEL_TAG_INDEX){
			mEventManager.refreshView((String)message.obj);
		}
	}

	private void updateWeatherInformation(String cityName){
		WeartherManager.getInstance().getRealWearther(new WeartherManager.WeatherCallback() {

			@Override
			public void onSuccess(Object object) {
				// TODO Auto-generated method stub
				refreshWeatherInfor((RealWeather)object);
			}

			@Override
			public void onFailure(Object object) {
				// TODO Auto-generated method stub
			}
		}, cityName);
	}

	private void refreshWeatherInfor(RealWeather realWeather){
		String weatherText = realWeather.getWeather() +
				" " +  realWeather.getTemperature();
		mWeatherText.setText(weatherText);

		String cityName = realWeather.getCity();
		mWeatherCity.setText(cityName);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if(requestCode == REQUEST_WEATHER_CODE && resultCode == 1){
			updateWeatherInformation(data.getStringExtra("city_name"));
		}else if(requestCode == REQUEST_CALENDAR_CODE && resultCode == 1){
			mCalendarLayout.showCurrentView();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}
}
